/**
 * 阅读记录相关API接口
 */

import type { PageResult } from './types/common'
import type {
  ReadingHistoryParams,
  ReadingRecordCreateReqVO,
  ReadingRecordRespVO,
  ReadingStatsRespVO,
  RecentReadingRespVO,
  TodayReadingRespVO,
} from './types/record'
import { http } from '@/utils/request'

/**
 * 保存阅读记录
 */
export function saveReadingRecord(data: ReadingRecordCreateReqVO) {
  return http.post<number>('/reading/reading-records/save', data)
}

/**
 * 获取绘本阅读记录
 */
export function getReadingRecord(bookId: number) {
  return http.get<ReadingRecordRespVO>(`/reading/reading-records/book/${bookId}`)
}

/**
 * 获取最近阅读记录
 */
export function getRecentReading(params?: { language?: string, limit?: number }) {
  // 将language参数转换为后端期望的lang参数
  const apiParams = params
    ? {
        lang: params.language,
        limit: params.limit,
      }
    : undefined
  return http.get<RecentReadingRespVO[]>('/reading/reading-records/recent', apiParams)
}

/**
 * 获取今日阅读记录
 */
export function getTodayReading() {
  return http.get<TodayReadingRespVO>('/reading/reading-records/today')
}

/**
 * 获取阅读统计数据
 */
export function getReadingStats() {
  return http.get<ReadingStatsRespVO>('/reading/reading-records/stats')
}

/**
 * 获取阅读历史
 */
export function getReadingHistory(params: ReadingHistoryParams) {
  return http.get<ReadingHistoryRespVO[]>('/reading/reading-records/history', params)
}

/**
 * 清空最近阅读记录
 */
export function clearRecentReading(language?: string) {
  return http.delete<ApiResponse<boolean>>('/reading/reading-records/recent/clear', { language })
}
